package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

public class SearchStuTeacher {
	private static List<Student> getStu(String name,int minAge,int maxAge) throws Exception{
		String sql="select * from t_students";
		int[] arr={0,0,0};
		String[] strs={"name","minAge","maxAge"};
		int count=0;
		String s ="";
		if(null!=name && !"".equals(name)){
			s+=" and name like ?";
			arr[0]=++count;
		}
		if(minAge>0){
			s+=" and age >= ?";
			arr[1]=++count;
		}
		if(maxAge>0 && maxAge>minAge){
			s+=" and age<=?";
			arr[2]=++count;
		}
		if(!"".equals(s)){
			sql+=" where "+s.substring(4);
		}
		
		Connection con=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		try{
			con=DBUtil.open();
			pst=con.prepareStatement(sql);
			if(0!=arr[0])
				pst.setString(arr[0], "%"+name+"%");
			if(0!=arr[1])
				pst.setInt(arr[1], minAge);
			if(0!=arr[2])
				pst.setInt(arr[2], maxAge);
		}finally{
			DBUtil.close(con, pst, rs);
		}
		return null;
	}
}
	
